package dk.modified.reshary;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.squareup.picasso.Picasso;

public class CommentActivityAdapter extends ArrayAdapter<Comment> {
	private Picasso picasso;
	private Context c;
	private int layoutResourceId;
	private ArrayList<Comment> data = null;
	private int[] rateClickHolder;
	private int userId;
	private buttonsClickedListener mButtonsClickedListener;
	private Integer smallImageSize;
	private Integer largeImageSize;

	public CommentActivityAdapter(Context context, int layoutResourceId,
			ArrayList<Comment> data) {
		super(context, layoutResourceId, data);
		this.c = context;
		this.data = data;
		this.layoutResourceId = layoutResourceId;
		rateClickHolder = new int[data.size()];
		picasso = PabloPicasso.with(context);
		
		smallImageSize = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,40,c.getResources().getDisplayMetrics()));
		largeImageSize = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,60,c.getResources().getDisplayMetrics()));
		
		SharedPreferences sharedpreferences = context.getSharedPreferences(
				"dk.modified.reshary", Context.MODE_PRIVATE);
		userId = Integer.parseInt(sharedpreferences.getString("userid", ""));
		
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		CommentViewHolder viewHolder = null;

		if (convertView == null) {
			LayoutInflater inflater = ((Activity) c).getLayoutInflater();
			convertView = inflater.inflate(layoutResourceId, parent, false);

			viewHolder = new CommentViewHolder();
			viewHolder.comment = (CommentView) convertView
					.findViewById(R.id.commentView);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (CommentViewHolder) convertView.getTag();
		}

		final Comment comment = data.get(position);
		if (comment != null) {
			CommentView currentComment = viewHolder.comment;
			currentComment.setName(comment.getRealname());
			currentComment.setComment(comment.getComment());
			currentComment.setDate(comment.getDate());
			currentComment.setRating(String.valueOf(comment.getRating()));

			if (comment.getParent() != 0) {
				picasso.load(comment.getProfilePic()).resize(smallImageSize, smallImageSize)
						.centerCrop().into(currentComment.getImageView());
				currentComment.showChild();
			} else {
				picasso.load(comment.getProfilePic()).resize(largeImageSize, largeImageSize)
						.centerCrop().into(currentComment.getImageView());
				currentComment.showParent();
			}
			if(userId == comment.getUserid()){
				currentComment.hideRateButtons();
			} else {
				currentComment.showRateButtons();
			}
			

			if (rateClickHolder[position] == CommentView.STATE_RATE_UP_CLICKED) {
				currentComment.setRateUpPressed();
			} else if (rateClickHolder[position] == CommentView.STATE_RATE_DOWN_CLICKED) {
				currentComment.setRateDownPressed();
			} else {
				currentComment.clearRates();
			}

			currentComment
					.setOnButtonsClickedListener(new buttonsClickedListener() {

						@Override
						public void replyClicked(Comment cmt, int pos) {
							
							mButtonsClickedListener.replyClicked(comment,position);
						}

						@Override
						public void rateUpClicked(int i, Comment cmt) {
							
							if (i == CommentView.STATE_DEACTIVATED) {
								rateClickHolder[position] = 0;
							} else {
								if(mButtonsClickedListener != null) mButtonsClickedListener.rateUpClicked(i,comment);
								rateClickHolder[position] = CommentView.STATE_RATE_UP_CLICKED;
							}
						}

						@Override
						public void rateDownClicked(int i, Comment cmt) {
							
							if (i == CommentView.STATE_DEACTIVATED) {
								rateClickHolder[position] = 0;
							} else {
								if(mButtonsClickedListener != null) mButtonsClickedListener.rateUpClicked(i,comment);
								rateClickHolder[position] = CommentView.STATE_RATE_DOWN_CLICKED;
							}
						}
					});
		}

		return convertView;
	}
	
	public void setOnButtonsClickedListener(buttonsClickedListener listener){
		mButtonsClickedListener = listener;
	}

	static class CommentViewHolder {
		CommentView comment;
	}
	
	public void addAll(int index,ArrayList<Comment> comments){
		data.addAll(index, comments);
		rateClickHolder = new int[data.size()];
		notifyDataSetChanged();
	}
	
}
